import sys
import datetime

from coin.strategy.mm.subscription import FeedSubscriptionRequest
from coin.strategy.mm.subscription import SubscriptionRequest
from coin.strategy.mm.tool.archive_base import run_from_archive
from coin.exchange.huobi_futures.kr_rest.futures_product import HuobiFuturesProduct


def run_bench(handlertype):
  machine = 'feed-01.ap-northeast-1.aws'
  start_time = datetime.datetime(2019, 9, 1, 0, 0)
  end_time = datetime.datetime(2019, 9, 1, 0, 3)

  global cnt
  cnt = 0

  product = HuobiFuturesProduct.FromStr('BTC-USD.QUARTER', current_datetime=start_time)
  feed_sub_request = FeedSubscriptionRequest()
  feed_sub_request.add_products([product], SubscriptionRequest('Futures', 'Huobi', None))

  def on_feed(feed):
    # 1. coin.exchange.bitmex.book_builder.BookBuilder
    # 2. MarketTradeEvent
    global cnt
    cnt += 1
    if cnt % 100 == 0:
      if handlertype:
        print(cnt, feed.ask0(), feed.bid0())
      else:
        print(cnt, feed.ask0().price, feed.bid0().price)

  def on_feed_reset(sub_key, book_builder):
    # feed_checker = book_builder._feed_handler.feed_checker
    book_builder.subscribe(product, on_feed)

  #   book_builder.subscribe_trade(product, on_feed)

  print(handlertype)
  run_from_archive(feed_sub_request,
                   on_feed_reset,
                   start_time,
                   end_time,
                   machine=[machine],
                   root_dir="/remote/iosg/coin/data/flow",
                   use_feed_arbitration=True,
                   worker_id=['1', '2'])

  print('count', cnt)


if __name__ == "__main__":
  if len(sys.argv) == 2:
    handlertype = sys.argv[1]
    assert handlertype == 'rust'
  else:
    handlertype = None

  run_bench(handlertype)
